import { login, logoutApi } from "@/api/sysuser";
import { getToken, removeToken, setToken } from "@/utils/token";
import { defineStore } from "pinia";


export const useUserStore = defineStore('user', {
    state: () => ({
        token: getToken() || ''
    }),
    actions: {
        setToken(token) {
            this.token = token
            setToken(token)
        },
        async logoutAction() {
            await logoutApi()
            this.logoutLocal()
        },
        logoutLocal() {
            this.token = '',
            removeToken()
        },
        async loginAction(loginForm) {
            const resp = await login(loginForm.userAccount, loginForm.password)
            const token = resp.data
            this.setToken(token)
        }
    }
})


